home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_parquet_tile.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  5.8 KB  |  171 lines

  1. /*
  2.  * DWParquetTile.sl -- yet another surface shader for wood
  3.  *-was:
  4.  * parquet_plank.sl -- another surface shader for wood.
  5.  *
  6.  * DESCRIPTION:
  7.  *   Makes texture of wooden planks in s-t space.  This wood looks rather
  8.  *   like oak plank parquet floor tiles.  The actual wood and plank pattern
  9.  *   is based on my "planks" shader.  This shader works best if "s" and "t"
  10.  *   units are both the same size in world space.
  11.  *
  12.  * PARAMETERS:
  13.  *   Ka, Kd, Ks, specular, roughness - work just like the plastic shader
  14.  *   txtscale - overall scaling factor for the texture
  15.  *   plankwidth - width of each plank (in terms of s/t)
  16.  *   plankspertile - number of planks in each parquet tile
  17.  *   ringscale - scaling for the ring spacing
  18.  *   grainscale - scaling for the fine grain
  19.  *   groovewidth - width of the grooves between the planks (in terms of s/t)
  20.  *   lightwood, darkwood - surface colors for the wood itself
  21.  *   groovecolor - the color of the "grooves" between the planks
  22.  *   plankvary - controls how much wood color varies from plank to plank
  23.  *   grainy - relative graininess (0 = no fine grain)
  24.  *   wavy - relative wavyness of the ring pattern
  25.  *
  26.  * ANTIALIASING: this shader does a pretty good job of antialiasing itself,
  27.  *   even with low sampling densities.
  28.  *
  29.  * AUTHOR: written by Larry Gritz, the George Washington University
  30.  *         email: gritz@SpamSucks_seas.gwu.edu
  31.  *         snail: Dept. of EE & CS
  32.  *                801  22nd St. NW, Rm. T-624-G
  33.  *                Washington, DC 20052
  34.  *
  35.  * HISTORY:
  36.  *    10 Feb 1995 - written by Larry Gritz, based on my "plank" shader.
  37.  *    10 Feb 1995 - modified by wave to change the name
  38.  *
  39.  * last modified 10 Feb 1995 by wave
  40.  * 
  41.  *
  42.  * modified again by Dan Weeks <dan@SpamSucks_mango.sfasu.edu> on 08 Dec 1996
  43.  *   - made one plank per tile like the flooring in our lab
  44.  *   - comments appear where changes are made
  45.  *   - many thanks to Larry Gritz and wave for creating the original
  46.  *
  47.  */
  48.  
  49.  
  50.  
  51. /*
  52.  * changed:
  53.  *   - name from LGParquetPlank to DWParquetTile
  54.  *   - ringscale from 15 to 25
  55.  *   - grainscale from 60 to 55
  56.  *   - plankspertile from 4 to 1
  57.  *   - plankwidth from .05 to .2
  58.  */
  59. surface
  60. k3d_parquet_tile (float Ka = 1, Kd = 0.75, Ks = .15, roughness = .025;
  61.            color specularcolor = 1;
  62.            float ringscale = 25, grainscale = 55;
  63.            float txtscale = 1;
  64.            float plankspertile = 1;
  65.            color lightwood = color (0.57, 0.292, 0.125);
  66.            color darkwood  = color (0.275, 0.15, 0.06);
  67.            color groovecolor  = color (.05, .04, .015);
  68.            float plankwidth = .2, groovewidth = 0.001;
  69.            float plankvary = 0.8;
  70.            float grainy = 1, wavy = 0.08; )
  71. {
  72. #define snoise(x) (2 * noise((x)) - 1)
  73. #define boxstep(a,b,x) (clamp(((x)-(a))/((b)-(a)),0,1))
  74. #define MINFILTERWIDTH 1.0e-7
  75.  
  76.   float r, r2;
  77.   point Nf;
  78.   float whichrow, whichplank;
  79.   float swidth, twidth, fwidth, ss, tt, w, h, fade, ttt;
  80.   color Ct, woodcolor;
  81.   float groovy;
  82.   float PGWIDTH, PGHEIGHT, GWF, GHF;
  83.   float tilewidth, whichtile, tmp, planklength;
  84.  
  85.   PGWIDTH = plankwidth+groovewidth;
  86.   planklength = PGWIDTH * plankspertile - groovewidth;
  87.   PGHEIGHT = planklength+groovewidth;
  88.   GWF = groovewidth*0.5/PGWIDTH;
  89.   GHF = groovewidth*0.5/PGHEIGHT;
  90.  
  91.   /* Determine how wide in s-t space one pixel projects to */
  92.   swidth = (max (abs(Du(s)*du) + abs(Dv(s)*dv), MINFILTERWIDTH) / PGWIDTH) * txtscale;
  93.   twidth = (max (abs(Du(t)*du) + abs(Dv(t)*dv), MINFILTERWIDTH) / PGHEIGHT) * txtscale;
  94.   fwidth = max(swidth,twidth);
  95.  
  96.   Nf = faceforward (normalize(N),I);
  97.  
  98.   ss = (txtscale * s) / PGWIDTH;
  99.   whichrow = floor (ss);
  100.   tt = (txtscale * t) / PGHEIGHT;
  101.   whichplank = floor(tt);
  102.   if (mod (whichrow/plankspertile + whichplank, 2) >= 1) {
  103.       ss = txtscale * t / PGWIDTH;
  104.       whichrow = floor (ss);
  105.       tt = txtscale * s / PGHEIGHT;
  106.       whichplank = floor(tt);
  107.       tmp = swidth;  swidth = twidth;  twidth = tmp;
  108.     } 
  109.   ss -= whichrow;
  110.   tt -= whichplank;
  111.   whichplank += 20*(whichrow+10);
  112.  
  113.   /*
  114.    * Figure out where the grooves are.  The value groovy is 0 where there
  115.    * are grooves, 1 where the wood grain is visible.  Do some simple
  116.    * antialiasing.
  117.    */
  118.   if (swidth >= 1)
  119.       w = 1 - 2*GWF;
  120.   else w = clamp (boxstep(GWF-swidth,GWF,ss), max(1-GWF/swidth,0), 1)
  121.      - clamp (boxstep(1-GWF-swidth,1-GWF,ss), 0, 2*GWF/swidth);
  122.   if (twidth >= 1)
  123.       h = 1 - 2*GHF;
  124.   else h = clamp (boxstep(GHF-twidth,GHF,tt), max(1-GHF/twidth,0),1)
  125.      - clamp (boxstep(1-GHF-twidth,1-GHF,tt), 0, 2*GHF/twidth);
  126.   /* This would be the non-antialiased version:
  127.    * w = step (GWF,ss) - step(1-GWF,ss);
  128.    * h = step (GHF,tt) - step(1-GHF,tt);
  129.    */
  130.   groovy = w*h;
  131.  
  132.  
  133.   /*
  134.    * Add the ring patterns
  135.    */
  136.   fade = smoothstep (1/ringscale, 8/ringscale, fwidth);
  137.   if (fade < 0.999) {
  138.       ttt = tt/4+whichplank/28.38 + wavy * noise (8*ss, tt/4);
  139.       r = ringscale * noise (ss-whichplank, ttt);
  140.       r -= floor (r);
  141.       r = 0.3 + 0.7 * smoothstep(0.2, 0.55, r) * (1 - smoothstep(0.75, 0.8, r));
  142.       r = (1-fade)*r + 0.65*fade;
  143.  
  144.       /*
  145.        * Multiply the ring pattern by the fine grain
  146.        */
  147.       fade = smoothstep (2/grainscale, 8/grainscale, fwidth);
  148.       if (fade < 0.999) {
  149.       r2 = 1.3 - noise (ss*grainscale, (tt*grainscale/4));
  150.       r2 = grainy * r2*r2 + (1-grainy);
  151.       r *= (1-fade)*r2 + (0.75*fade);
  152.         }
  153.       else r *= 0.75;
  154.     }
  155.   else r = 0.4875;
  156.   
  157.  
  158.   /* Mix the light and dark wood according to the grain pattern */
  159.   woodcolor = mix (lightwood, darkwood, r);
  160.  
  161.   /* Add plank-to-plank variation in overall color */
  162.   woodcolor *= (1-plankvary/2 + plankvary * noise (whichplank+0.5));
  163.  
  164.   Ct = mix (groovecolor, woodcolor, groovy);
  165.  
  166.   /* Use the plastic illumination model */
  167.   Oi = Os;
  168.   Ci = Os * ( Ct * (Ka*ambient() + Kd*diffuse(Nf)) +
  169.           specularcolor * Ks*specular(Nf,-normalize(I),roughness));
  170. }
  171.